import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class SpellCheckerConsoleUI {
    public static void main(String[] args) {
        SpellChecker spellChecker = new SpellChecker();
        
        System.out.println("Enter words to check. Enter \"quit\" to finish.");
        String word = promtForNewWord();
        while (!word.equals("quit")) {
            SpellCheckResult result = spellChecker.check(word);
            resultBehaviour(result, word);
            word = promtForNewWord();
        }
        System.out.println("Goodbye!");
    }
    
    public static String promtForNewWord() {
        System.out.print("Next word: ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        
        try {
            String word = reader.readLine();
            return word;
        } catch (IOException e) {
            return null;
        }
    }
    
    public static void resultBehaviour(SpellCheckResult result, String word) {
        if (result.isCorrect()) {
            System.out.println(word + " correct");
        } else {
            System.out.print(word + " not found. Nearest neighbour(s) ");
            String before = result.getBefore(), after = result.getAfter();
            if (before != null && after != null) {
                System.out.println(before + " and " + after);
            } else {
                System.out.println(before == null ? after : before);
            }
        }
    }
}
